package javapop.framework.parser;

import java.util.Arrays;
import java.util.List;

import javapop.framework.ParseContext;
import javapop.framework.ParseInput;
import javapop.framework.ParseNull;
import javapop.framework.ParseResult;
import javapop.framework.Parser;

public class ForgetAll extends Parser<Object> {
	private List<Parser<?>> children;
	
	public ForgetAll(Parser<?>...children) {
		if(children==null || children.length==0) {
			throw new IllegalArgumentException("No child parser");
		}
		this.children = Arrays.asList(children);
	}
	
	@Override
	public String getDescription() {
		StringBuilder buf = new StringBuilder();
		for(int i=0;i<children.size();i++) {
			buf.append(children.get(i));
			if(i<children.size())
				buf.append(" ; ");
		}
		return buf.toString();
	}
	
	@Override
	public boolean isMemoized() {
		return false;
	}
	
	@Override
	public ParseResult<?> eval(ParseContext<?> ctx, ParseInput input) {
		for(Parser<?> child : children) {
			ParseResult<?> result = child.parse(ctx,input);
			if(result.isError()) {
				return (ParseResult<?>) result.asError();
			}
		}
		
		return new ParseNull();
	}

}
